package interfazNonograma;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JOptionPane;

import nonograma.Nonograma;

public class TableroCanvas extends Canvas implements MouseListener{
	
	private static final int d = 10;
	private static final int tamCelda = 20;
	private int nFilas, nCol, maxFilas, maxCol;
	private Nonograma nonograma;
	
	public TableroCanvas(Nonograma tablero) {
		nonograma = tablero;
		nFilas = tablero.getFilas();
		nCol = tablero.getColumnas();
		maxFilas = tablero.getMaxFilas();
		maxCol = tablero.getMaxCol();
		this.addMouseListener(this);
		repaint();
	}

	
	public void paint(Graphics g) {
		Image fondo = getToolkit().getImage("images.jpg");
		g.drawImage(fondo,0,0, this);
		pintarTablero(g);
	}
	
	public void pintarTablero(Graphics g) {
		pintarCeldas(g);
		pintarCuadricula(g);
		/**Recorre el tablero y calcula las restricciones de fila y columna **/
		if (nonograma.isModoInverso()) { 
			nonograma.getRestriccionF();
			nonograma.getRestriccionC();
		}
		if (!nonograma.isModoNuevo()) {
			pintarRestrFilas(g);
			pintarRestrCol(g);
		}
	}

	private void pintarRestrCol(Graphics g) {
		int x = maxFilas*tamCelda + 2*d;
		int y = maxCol*tamCelda + d;
		for (int j=0; j<nCol; j++) {
			int k = 0;
			int n = nonograma.getIndiceC(j)-1;
			while (k<nonograma.getIndiceC(j)) {
				k++;
				g.setColor(Color.LIGHT_GRAY);
				g.fillRect(x+tamCelda*j, y-tamCelda*k, tamCelda, tamCelda);	
				g.setColor(Color.BLACK);
				g.drawRect(x+tamCelda*j, y-tamCelda*k, tamCelda, tamCelda);	
				Font font = new Font("Calibri",Font.BOLD,(tamCelda/3)*2+1);
				g.setFont(font);
				g.drawString(String.valueOf(nonograma.getElemRC(j,n)), x+tamCelda*j+tamCelda/4, y-tamCelda*k+tamCelda-tamCelda/5);
				n--;
			}
		}
	}

	private void pintarRestrFilas(Graphics g) {
		int x = maxFilas*tamCelda + d;
		int y = maxCol*tamCelda + 2*d;
		for (int i=0; i<nFilas; i++) {
			int k = 0;
			int n = nonograma.getIndiceF(i)-1;
			while (k<nonograma.getIndiceF(i)){ 
				k++;
				g.setColor(Color.LIGHT_GRAY);
				g.fillRect(x-tamCelda*k, y+tamCelda*i, tamCelda, tamCelda);
				g.setColor(Color.BLACK);
				g.drawRect(x-tamCelda*k, y+tamCelda*i, tamCelda, tamCelda);
				Font font = new Font("Calibri",Font.BOLD,(tamCelda/3)*2+1);
				g.setFont(font);
				g.drawString(String.valueOf(nonograma.getElemRF(i,n)), x-tamCelda*k+tamCelda/4, y+tamCelda*i+tamCelda-tamCelda/5);
				n--;
			}
		}
	}

	public void pintarCeldas(Graphics g) {
		int x = maxFilas * tamCelda + 2*d;
		int y = maxCol * tamCelda + 2*d;
		for (int i=0; i<nFilas; i++) {
			for (int j=0; j<nCol; j++) {
				if (nonograma.getElemTablero(i, j) == 0)
					g.setColor(Color.GRAY);
				else if (nonograma.getElemTablero(i, j) < 0)
					g.setColor(Color.WHITE);
				else if (nonograma.getElemTablero(i, j) > 0)
					g.setColor(Color.BLACK);
				
				g.fillRect(x+tamCelda*j, y+tamCelda*i, tamCelda, tamCelda);
			}
		}
	}
	
	
	public void pintarCuadricula(Graphics g) {
		int x1 = maxFilas * tamCelda + 2*d;
		int y1 = maxCol * tamCelda + 2*d;
		g.setColor(Color.BLACK);
 		for (int i=0; i<=nFilas; i++) //l�neas horizontales
			g.drawLine(x1, y1+tamCelda*i, x1+tamCelda*nCol, y1+tamCelda*i);
		for (int j=0; j<=nCol; j++) //l�neas verticales
			g.drawLine(x1+tamCelda*j, y1, x1+tamCelda*j, y1+tamCelda*nFilas);
	}


	public static int getTamcelda() {
		return tamCelda;
	} 
	
	public int getFilas() {
		return nFilas;
	}
	
	public int getCol() {
		return nCol;
	}
		
	public static int getD() {
		return d;
	}
	
	public int getmaxFilas() {
		return maxFilas;
	}
	
	public int getmaxCol() {
		return maxCol;
	}
	
	public Nonograma getNonograma() {
		return nonograma;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		int x =  e.getX() - (maxFilas*tamCelda + 2*d);
		int y = e.getY() - (maxCol*tamCelda + 2*d);
		try {
			int f = numCelda(y);
			int c = numCelda(x);
			int valor = nonograma.getElemTablero(f, c);
			valor++;
			if (valor == 2)
				valor = -1;
			nonograma.setElemTablero(f, c, valor);
			repaint();
		}
		catch (Exception ex) {
			JOptionPane.showMessageDialog(this, "Ha pinchado fuera del tablero", "ERROR", JOptionPane.ERROR_MESSAGE);
		}
	}

	private int numCelda(int y) {
		return y/tamCelda;
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
